#include "project.h"
bool is_start = false;
bool should_exit = false;
bool is_arrang = false;

void *showtime(void *arg)
{
    if (arg == nullptr) {
        cerr << "ERROR: framebuffer_ptr is null!" << endl;
        return NULL;
    }
    Timeshow &time = Timeshow::getTime();
    time.lcd_mmap_ptr = (char*)arg;
    if (time.lcd_mmap_ptr == nullptr) {
        cerr << "lcd_mmap_ptr is null!" << endl;
        return NULL;
    }
    while(1)
    {
        time.show_time();
        usleep(1000);
    }
    return NULL;
}

int main()
{
    // 基类工厂�?     指向    派生类工厂类
    shared_ptr<DEVICE> D_show = make_shared<LCD>("/dev/fb0");
    shared_ptr<DEVICE> D_get = make_shared<TOUCH>("/dev/input/event6");
    // 基类产品�?     指向    派生类产品类
    auto display = D_show->create_display();
    auto touch_handler = D_get->create_touch_handler();

    auto *image_observer = dynamic_cast<Image *>(display.get()); //.get(),从智能指针变成普通指针，才能进�?�转�?
    auto *touch_subject = dynamic_cast<Get_touch *>(touch_handler.get());

    // 内存还是�? display �? touch_handler 这两�? shared_ptr 管理�?
    // image_observer �? touch_subject �?�?“临时指向”这些�?�象
    // �? main() 结束时，display �? touch_handler 会�??销�? �? �?动释放内�?

    if (image_observer && touch_subject)
    {
        // 加入观察�?
        touch_subject->attach(image_observer);
    }
    else
    {
        cerr << "Failed to cast to Image or Get_touch" << endl;
        return -1;
    }

    display->Showfill_image(START_PATH);
    is_start = true;

    pthread_t tid;
    pthread_create(&tid, NULL, showtime, image_observer->framebuffer_ptr);
    pthread_detach(tid);

    while (!should_exit)
    {
        touch_handler->Get_Touch_Inf();
    }

    return 0;
}